﻿namespace SmartSoft.DAL
{
    using System;
    using System.Data;
    using System.Data.SqlClient;using NLog;

    public class DmDmSpDataAccess
    {
        private static Logger log = LogManager.GetCurrentClassLogger();
        public DataSet GetDataSet(int pi_WorkingYear)
        {
            DataSet set2;
            SqlParameter[] values = new SqlParameter[] { new SqlParameter("p_NamDM", pi_WorkingYear) };
            SqlConnection SSConnection = SSDataAccess.SSConnection;
            DataSet dataSet = new DataSet("VBSP");
            SqlDataAdapter adapter = new SqlDataAdapter();
            SqlCommand command = new SqlCommand();
            try
            {
                command.CommandText = "DinhMuc_Get";
                command.CommandType = CommandType.StoredProcedure;
                command.Connection = SSConnection;
                command.Parameters.AddRange(values);
                adapter.SelectCommand = command;
                adapter.Fill(dataSet, "ITC");
                set2 = dataSet;
            }
            catch (Exception exception)
            {
                log.ErrorException(string.Empty, exception); 
                throw new NotSupportedException("Invalid expression", exception);
            }
            finally
            {
                SSDataAccess.Close(SSConnection);
            }
            return set2;
        }

        public DataSet GetDataSet(int pi_WorkingYear, string ps_Code)
        {
            DataSet set2;
            SqlParameter[] values = new SqlParameter[] { new SqlParameter("p_NamDM", pi_WorkingYear), new SqlParameter("p_Ma_Sp", ps_Code) };
            SqlConnection SSConnection = SSDataAccess.SSConnection;
            DataSet dataSet = new DataSet("VBSP");
            SqlDataAdapter adapter = new SqlDataAdapter();
            SqlCommand command = new SqlCommand();
            try
            {
                command.CommandText = "DinhMuc_Get";
                command.CommandType = CommandType.StoredProcedure;
                command.Connection = SSConnection;
                command.Parameters.AddRange(values);
                adapter.SelectCommand = command;
                adapter.Fill(dataSet, "ITC");
                set2 = dataSet;
            }
            catch (Exception exception)
            {
                log.ErrorException(string.Empty, exception); 
                throw new NotSupportedException("Invalid expression", exception);
            }
            finally
            {
                SSDataAccess.Close(SSConnection);
            }
            return set2;
        }

        public int SaveRow(bool pb_NewChecked, DataRow row)
        {
            int num;
            SqlParameter[] values = new SqlParameter[] { 
                new SqlParameter("p_Moi_Sua", pb_NewChecked), new SqlParameter("p_Stt", row["Stt"]), new SqlParameter("p_NamDM", row["NamDM"]), new SqlParameter("p_Loai_Yt", row["Loai_Yt"]), new SqlParameter("p_Ma_Vt_Nh", row["Ma_Vt_Nh"]), new SqlParameter("p_Ma_Sp", row["Ma_Sp"]), new SqlParameter("p_Ma_Dm_Sp", row["Ma_Dm_Sp"]), new SqlParameter("p_So_LuongSP", row["So_LuongSP"]), new SqlParameter("p_Dinh_Muc01", row["Dinh_Muc01"]), new SqlParameter("p_Dinh_Muc02", row["Dinh_Muc02"]), new SqlParameter("p_Dinh_Muc03", row["Dinh_Muc03"]), new SqlParameter("p_Dinh_Muc04", row["Dinh_Muc04"]), new SqlParameter("p_Dinh_Muc05", row["Dinh_Muc05"]), new SqlParameter("p_Dinh_Muc06", row["Dinh_Muc06"]), new SqlParameter("p_Dinh_Muc07", row["Dinh_Muc07"]), new SqlParameter("p_Dinh_Muc08", row["Dinh_Muc08"]), 
                new SqlParameter("p_Dinh_Muc09", row["Dinh_Muc09"]), new SqlParameter("p_Dinh_Muc10", row["Dinh_Muc10"]), new SqlParameter("p_Dinh_Muc11", row["Dinh_Muc11"]), new SqlParameter("p_Dinh_Muc12", row["Dinh_Muc12"]), new SqlParameter("p_Gia_Nt", row["Gia_Nt"]), new SqlParameter("p_Tien_Nt", row["Tien_Nt"]), new SqlParameter("p_H_Luong", row["H_Luong"]), new SqlParameter("p_Do_Am_PT", row["Do_Am_PT"]), new SqlParameter("p_Do_Con_PT", row["Do_Con_PT"]), new SqlParameter("p_Ma_Loai1", row["Ma_Loai1"]), new SqlParameter("p_Ma_Loai2", row["Ma_Loai2"]), new SqlParameter("p_Ma_Loai3", row["Ma_Loai3"]), new SqlParameter("p_Result", SqlDbType.BigInt)
             };
            values[1].Direction = ParameterDirection.InputOutput;
            values[1].SqlDbType = SqlDbType.BigInt;
            values[0x1c].Direction = ParameterDirection.Output;
            values[0x1c].SqlDbType = SqlDbType.Int;
            SqlConnection SSConnection = SSDataAccess.SSConnection;
            SqlCommand command = new SqlCommand();
            try
            {
                command.CommandText = "DinhMuc_Save";
                command.CommandType = CommandType.StoredProcedure;
                command.Connection = SSConnection;
                command.Parameters.AddRange(values);
                command.ExecuteNonQuery();
                row["Stt"] = values[1].Value;
                num = (int) values[0x1c].Value;
            }
            catch (Exception exception)
            {
                log.ErrorException(string.Empty, exception); 
                throw new NotSupportedException("Invalid expression", exception);
            }
            finally
            {
                SSDataAccess.Close(SSConnection);
            }
            return num;
        }
    }
}

